import { customRef } from 'vue'

export function debounceRef(value, delay = 1000) {
  let timer
  return customRef((track, trigger) => {
    return {
      get() {
        track() //收集依赖
        return value
      },
      set(val) {
        clearTimeout(timer)
        timer = setTimeout(() => {
          trigger() //通知依赖
          value = val
        }, delay)
      }
    }
  })
}
